package com.codedancer.campus.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.common.utils.UUIDGenerator;
import com.codedancer.campus.manager.service.ICategoryService;
import com.codedancer.campus.manager.service.bo.category.CategoryAddOrUpdateBo;
import com.codedancer.campus.manager.service.bo.category.CategoryPageSearchBo;
import com.codedancer.campus.manager.service.vo.category.CategoryPageVo;
import com.codedancer.campus.mapper.CategoryMapper;
import com.codedancer.campus.mapper.CommodityMapper;
import com.codedancer.campus.mapper.PlateMapper;
import com.codedancer.campus.po.Category;
import com.codedancer.campus.qo.category.CategoryPageSearchQo;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private PlateMapper plateMapper;

    /**
     * 分页查询分类
     *
     * @param categoryPageSearchBo
     * @return
     */
    @Override
    public IPage<CategoryPageVo> page(CategoryPageSearchBo categoryPageSearchBo) {
        CategoryPageSearchQo categoryPageSearchQo = BeanUtil.copyProperties(categoryPageSearchBo,
            CategoryPageSearchQo.class);
        IPage<Category> categoryIPage = categoryMapper.page2Manager(
            new Page<Category>(categoryPageSearchBo.getPage(), categoryPageSearchBo.getSize()),
            categoryPageSearchQo);
        IPage<CategoryPageVo> categoryPageVoIPage = new Page<>();
        List<CategoryPageVo> categoryPageVos;
        categoryPageVos = categoryIPage.getRecords().stream()
            .map(category -> {
                CategoryPageVo categoryPageVo = BeanUtil.copyProperties(category,
                    CategoryPageVo.class);
                categoryPageVo.setPlateName(category.getPlate().getName());
                categoryPageVo.setPlateUuid(category.getPlate().getUuid());
                return categoryPageVo;
            }).collect(Collectors.toList());
        categoryPageVoIPage.setRecords(categoryPageVos);
        categoryPageVoIPage.setTotal(categoryIPage.getTotal());
        return categoryPageVoIPage;
    }

    /**
     * 删除分类
     *
     * @param uuid
     */
    @Override
    public void delete(String uuid) {
        if (!categoryMapper.existsByUuId(uuid)) {
            throw new BusinessException("分类不存在");
        }
        if (commodityMapper.existsByColumn("category_uuid", uuid)) {
            throw new BusinessException("该分类已被使用，不能删除");
        }
        // 删除分类
        categoryMapper.deleteById(uuid);
    }

    /**
     * 添加分类
     *
     * @param categoryAddOrUpdateBo
     */
    @Override
    public void add(CategoryAddOrUpdateBo categoryAddOrUpdateBo) {
        if (!plateMapper.existsByUuId(categoryAddOrUpdateBo.getPlateUuid())) {
            throw new BusinessException("板块不存在");
        }
        // 判断该板块下是否存在分类
        if (categoryMapper.existsByPlateUuid(categoryAddOrUpdateBo.getPlateUuid(),
            categoryAddOrUpdateBo.getName())) {
            throw new BusinessException("该板块下分类名称已存在，不能添加");
        }
        Category category = BeanUtil.copyProperties(categoryAddOrUpdateBo, Category.class);
        category.setUuid(UUIDGenerator.getUUID());
        categoryMapper.insert(category);
    }

    /**
     * 修改分类
     *
     * @param categoryAddOrUpdateBo
     */
    @Override
    public void update(CategoryAddOrUpdateBo categoryAddOrUpdateBo) {
        Category category = categoryMapper.findById(categoryAddOrUpdateBo.getUuid())
            .orElseThrow(() -> new BusinessException("分类不存在"));
        if (!plateMapper.existsByUuId(categoryAddOrUpdateBo.getPlateUuid())) {
            throw new BusinessException("板块不存在");
        }
        if (!category.getName().equals(categoryAddOrUpdateBo.getName())) {
            // 分类名称不一致
            if (categoryMapper.existsByPlateUuid(categoryAddOrUpdateBo.getPlateUuid(),
                categoryAddOrUpdateBo.getName())) {
                throw new BusinessException("该板块下分类名称已存在，不能添加");
            }
            category.setName(categoryAddOrUpdateBo.getName());
        }
        category.setPlateUuid(categoryAddOrUpdateBo.getPlateUuid());
        categoryMapper.updateById(category);
    }
}
